﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Argos.Scheduling;
using System.ComponentModel.Design;
using System.Drawing.Design;

namespace Argos.Windows.Forms
{
	public partial class WeeklyScheduleEditor: UserControl
	{
		public WeeklyScheduleEditor()
		{
			InitializeComponent();
			this.Slots = new SlotListContainer();
		}

		[Browsable(false)]
		[DefaultValue(null)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public WeeklySchedule SelectedSchedule
		{
			get
			{
				WeeklySchedule schedule = new WeeklySchedule();

				WeekdayList list = new WeekdayList();
				if (this.MondayCheckbox.Checked) list.Add(DayOfWeek.Monday);
				if (this.TuesdayCheckbox.Checked) list.Add(DayOfWeek.Tuesday);
				if (this.WednesdayCheckbox.Checked) list.Add(DayOfWeek.Wednesday);
				if (this.ThursdayCheckbox.Checked) list.Add(DayOfWeek.Thursday);
				if (this.FridayCheckbox.Checked) list.Add(DayOfWeek.Friday);
				if (this.SaturdayCheckbox.Checked) list.Add(DayOfWeek.Saturday);
				if (this.SundayCheckbox.Checked) list.Add(DayOfWeek.Sunday);

				foreach (DayOfWeek day in list)
				{
					foreach (TimeSlot slot in this.Slots.SlotList)
					{
						schedule.Entries.Add(new WeeklySchedule.Entry(day, slot.Start, slot.Duration));
					}
				}

				return schedule;
			}
			set
			{
				if (null != value && !object.Equals(this.SelectedSchedule, value))
				{
					this.MondayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Monday);
					this.TuesdayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Tuesday);
					this.WednesdayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Wednesday);
					this.ThursdayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Thursday);
					this.FridayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Friday);
					this.SaturdayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Saturday);
					this.SundayCheckbox.Checked = null != value.Entries.FirstOrDefault(e => ((WeeklySchedule.Entry)e).Day == DayOfWeek.Sunday);

					this.Slots.SlotList.Clear();

					foreach (WeeklySchedule.Entry entry in value.Entries)
					{
						TimeSlot slot = TimeSlot.FromStartTime(entry.Start, entry.Duration);
						if (!this.Slots.SlotList.Contains(slot))
						{
							this.Slots.SlotList.Add(slot);
						}
					}
				}
				else
				{
					this.MondayCheckbox.Checked = false;
					this.TuesdayCheckbox.Checked = false;
					this.WednesdayCheckbox.Checked = false;
					this.ThursdayCheckbox.Checked = false;
					this.FridayCheckbox.Checked = false;
					this.SaturdayCheckbox.Checked = false;
					this.SundayCheckbox.Checked = false;
					this.SelectAllCheckbox.Checked = false;

					this.Slots.SlotList.Clear();
				}
			}
		}

		private SlotListContainer Slots
		{
			get { return this.TimeSlotsPropertyEditor.SelectedObject as SlotListContainer; }
			set { this.TimeSlotsPropertyEditor.SelectedObject = value; }
		}

		private void SelectAllCheckbox_CheckedChanged(object sender, EventArgs e)
		{
			this.MondayCheckbox.Checked = this.SelectAllCheckbox.Checked;
			this.TuesdayCheckbox.Checked = this.SelectAllCheckbox.Checked;
			this.WednesdayCheckbox.Checked = this.SelectAllCheckbox.Checked;
			this.ThursdayCheckbox.Checked = this.SelectAllCheckbox.Checked;
			this.FridayCheckbox.Checked = this.SelectAllCheckbox.Checked;
			this.SaturdayCheckbox.Checked = this.SelectAllCheckbox.Checked;
			this.SundayCheckbox.Checked = this.SelectAllCheckbox.Checked;
		}


		private class SlotListContainer
		{
			public SlotListContainer()
			{
				this.SlotList = new List<TimeSlot>();
			}

			[Category("Schedule")]
			[DisplayName("Time Slots")]
			[Description("Specifies the time slots for the weekly schedule.")]
			[TypeConverter(typeof(TimeSlotListConverter))]
			public List<TimeSlot> SlotList { get; set; }
		}

		private class TimeSlotListConverter : TypeConverter
		{
			public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
			{
				return destinationType == typeof(string);
			}

			public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
			{
				if (null == destinationType)
				{
					throw new ArgumentNullException("destinationType");
				}

				if (value is List<TimeSlot>)
				{
					List<TimeSlot> list = (List<TimeSlot>)value;
					if (destinationType == typeof(string))
					{
						TimeSlotList slots = new TimeSlotList(list);
						return slots.ToString();
					}
				}

				return base.ConvertTo(context, culture, value, destinationType);
			}
		}

	}
}
